package com.lqz.tools;
import java.sql.*;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DataSourceUtils;

import com.alibaba.druid.pool.DruidDataSource;

public class DBSql2008con {

	/**
	 * @param args
	 * @throws NamingException 
	 * @throws SQLException 
	 */
		
	public static Connection getCon() throws NamingException, SQLException {
		
		DruidDataSource dataSource = (DruidDataSource)ApplicationContextHelper.getBean("dataSource");  
		Connection connection = DataSourceUtils.getConnection(dataSource);  
//		DataSource ds = null;
//		InitialContext ctx = new InitialContext();
//		ds = (DataSource)ctx.lookup("java:comp/env/jdbc/DB_DR");
//		Connection conn = ds.getConnection();
//		Connection con = null;
//		try{                                                                     
//			String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//		      Class.forName(driverName);                                    
//
//		      String url="jdbc:sqlserver://10.10.10.134:1433;DatabaseName=DB_DR";
//		      String user="sa";
//		      String passwd="123456";
//		      
//		      con=DriverManager.getConnection(url,user,passwd);
//		} catch(Exception e){
////			 System.out.println("shibSQL驱动程序");
//			e.printStackTrace();
//		}
		return connection;                              
	}
	public static void  closecon(Connection con,PreparedStatement ps,ResultSet rs,Statement cs) {
		
		try {
			if(rs!=null){
				rs.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(con!=null){
				con.close();
			}
			if(cs!=null){
				cs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] a){
		try{
			Connection con=getCon();
			String sql="select id,src,name,count from music_src where 1=1";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				System.out.println(rs.getString("id"));
			}
			rs.close();
			st.close();
			con.close();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	
}
